home *** CD-ROM | disk | FTP | other *** search
- Imports System.Xml
- Imports System.Xml.Xsl
-
- Public Class DomForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.IContainer
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- Friend WithEvents btnLoad As System.Windows.Forms.Button
- Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
- Friend WithEvents btnRemove As System.Windows.Forms.Button
- Friend WithEvents btnCreate As System.Windows.Forms.Button
- Friend WithEvents btnSearch As System.Windows.Forms.Button
- Friend WithEvents txtXPath As System.Windows.Forms.TextBox
- Friend WithEvents btnTransform As System.Windows.Forms.Button
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.btnLoad = New System.Windows.Forms.Button()
- Me.TreeView1 = New System.Windows.Forms.TreeView()
- Me.btnRemove = New System.Windows.Forms.Button()
- Me.btnCreate = New System.Windows.Forms.Button()
- Me.btnSearch = New System.Windows.Forms.Button()
- Me.txtXPath = New System.Windows.Forms.TextBox()
- Me.btnTransform = New System.Windows.Forms.Button()
- Me.SuspendLayout()
- '
- 'btnLoad
- '
- Me.btnLoad.Location = New System.Drawing.Point(8, 16)
- Me.btnLoad.Name = "btnLoad"
- Me.btnLoad.Size = New System.Drawing.Size(96, 40)
- Me.btnLoad.TabIndex = 0
- Me.btnLoad.Text = "Load and display"
- '
- 'TreeView1
- '
- Me.TreeView1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
- Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.TreeView1.ImageIndex = -1
- Me.TreeView1.Location = New System.Drawing.Point(120, 16)
- Me.TreeView1.Name = "TreeView1"
- Me.TreeView1.SelectedImageIndex = -1
- Me.TreeView1.Size = New System.Drawing.Size(384, 296)
- Me.TreeView1.TabIndex = 1
- '
- 'btnRemove
- '
- Me.btnRemove.Location = New System.Drawing.Point(8, 72)
- Me.btnRemove.Name = "btnRemove"
- Me.btnRemove.Size = New System.Drawing.Size(96, 40)
- Me.btnRemove.TabIndex = 2
- Me.btnRemove.Text = "Remove Nodes "
- '
- 'btnCreate
- '
- Me.btnCreate.Location = New System.Drawing.Point(8, 128)
- Me.btnCreate.Name = "btnCreate"
- Me.btnCreate.Size = New System.Drawing.Size(96, 40)
- Me.btnCreate.TabIndex = 3
- Me.btnCreate.Text = "Create Nodes"
- '
- 'btnSearch
- '
- Me.btnSearch.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
- Me.btnSearch.Location = New System.Drawing.Point(8, 312)
- Me.btnSearch.Name = "btnSearch"
- Me.btnSearch.Size = New System.Drawing.Size(96, 40)
- Me.btnSearch.TabIndex = 4
- Me.btnSearch.Text = "Search Nodes"
- '
- 'txtXPath
- '
- Me.txtXPath.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.txtXPath.Location = New System.Drawing.Point(120, 320)
- Me.txtXPath.Name = "txtXPath"
- Me.txtXPath.Size = New System.Drawing.Size(384, 24)
- Me.txtXPath.TabIndex = 5
- Me.txtXPath.Text = ""
- '
- 'btnTransform
- '
- Me.btnTransform.Location = New System.Drawing.Point(8, 184)
- Me.btnTransform.Name = "btnTransform"
- Me.btnTransform.Size = New System.Drawing.Size(96, 40)
- Me.btnTransform.TabIndex = 6
- Me.btnTransform.Text = "Transform wtih XSLT"
- '
- 'DomForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(520, 357)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnTransform, Me.txtXPath, Me.btnSearch, Me.btnCreate, Me.btnRemove, Me.TreeView1, Me.btnLoad})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Name = "DomForm"
- Me.Text = "XmlDocument class"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- ' load the XML file
-
- Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
- DisplayXmlFile("employees.xml", TreeView1)
- End Sub
-
- ' remove some elements
-
- Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
- ' Load the XML document.
- Dim xmldoc As New XmlDocument()
- xmldoc.Load("employees.xml")
-
- ' Remove the ID attribute and the birthDate sub-element from all
- ' Employee elements.
- Dim xmlEl As XmlElement
- For Each xmlEl In xmldoc.DocumentElement.ChildNodes
- ' remove the attribute with a given name.
- xmlEl.RemoveAttribute("id")
- ' Get a reference to the birthDate sub-element.
- Dim xmlList As XmlNodeList = xmlEl.GetElementsByTagName("birthDate")
- If xmlList.Count > 0 Then
- xmlEl.RemoveChild(xmlList(0))
- End If
- Next
-
- ' Add it to the TreeView Nodes collection
- DisplayXmlNode(xmldoc, TreeView1.Nodes)
- End Sub
-
-
-
- Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
- ' Load the XML document.
- Dim xmldoc As New XmlDocument()
- xmldoc.Load("employees.xml")
-
- #Const USE_CREATEAPPEND_ROUTINE = False
-
- #If USE_CREATEAPPEND_ROUTINE Then
- ' Create a new Employee element
- Dim xmlEl As XmlElement = xmldoc.CreateElement("Employee")
- ' Append it to the children collection of the DOM root element.
- xmldoc.DocumentElement.AppendChild(xmlEl)
-
- ' Set the ID attribute of the new element.
- xmlEl.SetAttribute("id", "100")
- ' Create all its sub-elements.
- Dim xmlChildEl As XmlElement
-
- ' This is the firstName sub-element.
- xmlChildEl = xmldoc.CreateElement("firstName")
- ' Create a child XmlText element for the firstName element.
- Dim xmlText As XmlText = xmldoc.CreateTextNode("Joe")
- xmlChildEl.AppendChild(xmlText)
- ' Append the firstName element to the new Employee element.
- xmlEl.AppendChild(xmlChildEl)
-
- ' This is the lastName sub-element.
- xmlChildEl = xmldoc.CreateElement("lastName")
- ' Create a child XmlText element and append it in one operation.
- xmlChildEl.AppendChild(xmldoc.CreateTextNode("Doe"))
- ' Append the lastName sub-Element to the new Employee element.
- xmlEl.AppendChild(xmlChildEl)
-
- ' This is the address sub-element
- xmlChildEl = xmldoc.CreateElement("address")
- ' This time we use the InnerText property (a Microsoft extension to WC3 DOM).
- xmlChildEl.InnerText = "1234 North Street"
- ' Append the address sub-element to the new Employee element.
- xmlEl.AppendChild(xmlChildEl)
-
- ' This is the city sub-element.
- xmlChildEl = xmldoc.CreateElement("city")
- xmlChildEl.InnerText = "Boston"
- xmlEl.AppendChild(xmlChildEl)
- #Else
- ' This version uses the CreateAppend auxiliary routine.
-
- ' Create a new Employee element.
- Dim xmlEl As XmlElement = CreateAppendElement(xmldoc.DocumentElement, "Employee")
- ' Set the ID attribute of the new element.
- xmlEl.SetAttribute("id", "100")
- ' Create all its sub-elements.
- CreateAppendElement(xmlEl, "firstName", "Joe")
- CreateAppendElement(xmlEl, "lastName", "Doe")
- CreateAppendElement(xmlEl, "address", "1234 North Street")
- CreateAppendElement(xmlEl, "city", "Boston")
- #End If
-
- ' Add it to the TreeView Nodes collection
- DisplayXmlNode(xmldoc, TreeView1.Nodes)
-
- ' Save to a different file
- xmldoc.Save("employees2.xml")
- End Sub
-
- ' make an XPath search
-
- Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
- ' Load the XML document.
- Dim xmldoc As New XmlDocument()
- xmldoc.Load("employees.xml")
-
- Dim xpath As String = txtXPath.Text
- Dim xnl As XmlNodeList
- Try
- xnl = xmldoc.SelectNodes(xpath)
- Catch ex As Exception
- MessageBox.Show(ex.Message, "Invalid XPath syntax", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Exit Sub
- End Try
- Dim xn As XmlNode
- Dim msg As String = ""
-
- For Each xn In xnl
- msg &= xn.Name
- If Not (xn.Value Is Nothing) Then msg &= "(" & xn.Value.ToString & ")"
- msg &= ControlChars.CrLf
-
- Dim xmlEl As XmlElement = CType(xn, XmlElement)
- Debug.WriteLine(xmlEl.GetElementsByTagName("lastName")(0).InnerText())
- Next
- MessageBox.Show(msg, "Found " & xnl.Count.ToString & " matches")
-
- End Sub
-
- ' make an XSL transform
-
- Private Sub btnTransform_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTransform.Click
- ' Load the XSLT into an XmlTransform.
- Dim xslTran As New XslTransform()
- ' Load the .xslt file into it.
- xslTran.Load("Employees.xslt")
-
- ' required action
- ' 0=convert to an HTML file
- ' 1= use an XmlTextWriter
- ' 2= create an XmlReader object
-
- #Const ACTION = 2
-
- #If ACTION = 0 Then
- ' Convert the XML file into another XML file.
- xslTran.Transform("employees.xml", "employees.html")
- #ElseIf ACTION = 1 Then
- ' Load the XML document.
- Dim xmldoc As New XmlDocument()
- xmldoc.Load("employees.xml")
-
- Dim xtw As New XmlTextWriter("employees.html", System.Text.Encoding.UTF8)
- xslTran.Transform(xmldoc.CreateNavigator, Nothing, xtw)
- xtw.Close()
- #Else
- ' Load the XML document.
- Dim xmldoc As New XmlDocument()
- xmldoc.Load("employees.xml")
-
- Dim xr As XmlReader = xslTran.Transform(xmldoc.CreateNavigator, Nothing)
- Dim xmldoc2 As New XmlDocument()
- xmldoc2.Load(xr)
- xr.Close()
- DisplayXmlNode(xmldoc2, TreeView1.Nodes)
- #End If
-
- End Sub
- End Class
-
- ' common routines
-
- Module XmlTreeViewSubs
- ' display an XML file in a TreeView
-
- Sub DisplayXmlFile(ByVal filename As String, ByVal tvw As TreeView)
- Dim xmldoc As New XmlDocument()
- xmldoc.Load(filename)
- ' Add it to the TreeView Nodes collection
- DisplayXmlNode(xmldoc, tvw.Nodes)
- ' Expand the root node.
- tvw.Nodes(0).Expand()
- End Sub
-
- Sub DisplayXmlNode(ByVal xmlnode As XmlNode, ByVal nodes As TreeNodeCollection)
- ' Add a TreeView node for this XmlNode.
- ' (Using the node's Name is OK for most XmlNode types.)
- Dim tvNode As TreeNode = nodes.Add(xmlnode.Name)
-
- Select Case xmlnode.NodeType
- Case XmlNodeType.Element
- ' This is an element: Check whether there are attributes.
- If xmlnode.Attributes.Count > 0 Then
- ' Create an ATTRIBUTES node.
- Dim attrNode As TreeNode = tvNode.Nodes.Add("(ATTRIBUTES)")
- ' Add all the attributes as children of the new node.
- Dim xmlAttr As XmlAttribute
- For Each xmlAttr In xmlnode.Attributes
- ' Each node shows name and value.
- attrNode.Nodes.Add(xmlAttr.Name & " = '" & xmlAttr.Value & "'")
- Next
- End If
- Case XmlNodeType.Text, XmlNodeType.CDATA
- ' For these node types we display the value
- tvNode.Text = xmlnode.Value
- Case XmlNodeType.Comment
- tvNode.Text = "<!--" & xmlnode.Value & "-->"
- Case XmlNodeType.ProcessingInstruction, XmlNodeType.XmlDeclaration
- tvNode.Text = "<?" & xmlnode.Name & " " & xmlnode.Value & "?>"
- Case Else
- ' ignore other node types.
- End Select
-
- ' Call this routine recursively for each child node.
- Dim xmlChild As XmlNode = xmlnode.FirstChild
- Do Until xmlChild Is Nothing
- DisplayXmlNode(xmlChild, tvNode.Nodes)
- xmlChild = xmlChild.NextSibling
- Loop
- End Sub
-
- ' Create an XmlElement object with inner text and make it
- ' a child of another XmlNode.
-
- Function CreateAppendElement(ByVal parentNode As XmlNode, ByVal name As String, Optional ByVal innerText As String = Nothing) As XmlElement
- ' Create a new XmlElement object, set the return value.
- Dim xmlEl As XmlElement = parentNode.OwnerDocument.CreateElement(name)
- ' Set its inner text
- If Not (innerText Is Nothing) Then xmlEl.InnerText = innerText
- ' make it a child of its parent node.
- parentNode.AppendChild(xmlEl)
- ' Return the new node to the caller
- Return xmlEl
- End Function
-
- End Module